// vp/vp_sc/pages/index/index.js
var app = getApp();
var vp = require('../../resource/js/vp.js');

const innerAudioContext = wx.createInnerAudioContext();

Page({
        /**
         * 页面的初始数据
         */
        data: {
                signed:null,
                _signModal:false,

                _shareModal:false,

                addmy:false,
                noticed:0,// 我上次关闭的公告的ID，当前公告时间戳大于该数值才显示

                followed:null,
                _followedModal:false,

                cats:null,

                _searchModal:false,
                search:'',      // 搜索框中的内容
                searching:'', // 当前搜索的内容

                cat:'',
                start: 0,
                more: 1,
                list: null,
                is_loading_more: false,
                
                // 当前播放的喇叭
                horn:null,
                horn_audio_playing:false,

                // 当前查看的商户
                puber:null,

                // 管理员
                _shieldModal:false,
				dialogShow: false,
				vipcode:'',
				msgInfo:"您正在注册半叶堂医疗平台家庭医生会员，注册后即可享受多项会员权益。点击确定完成注册",
				vipstatus:1
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
			// 读取参数
			
			var self = this;
			const params = app.globalData.launchParams;
			if (params) {
				console.log("params:",params)
				let nparams = decodeURIComponent(params.scene)
				const [key, value] = nparams.split('=');
				if(key == 'vipcode'){
				  let vipcode = value
				  console.log(vipcode)
				  self.setData({
					vipcode:vipcode
				  })
				}
			}
			const enterOptions = wx.getEnterOptionsSync(); // 获取本次进入小程序的场景值等信息
			const { scene, path, shareTicket } = enterOptions;
			
			// 场景值 1154 表示来自朋友圈单页模式:cite[6]
			// if (scene === 1154) {
				
			// 	console.log('index:',scene)
				
			// 	wx.redirectTo({
			// 		url: '/vp_pz/pages/index/index',
			// 		success: function(res) {
			// 			console.log('跳转成功');
			// 		  },
			// 		  fail: function(err) {
			// 			console.log('跳转失败', err);
			// 		  },
			// 		  complete: function() {
			// 			console.log('跳转操作完成');
			// 		  }
			// 	  });
			// 	  return
			  // 处于朋友圈单页模式，设置状态但不自动跳转
			  // 可以在这里执行一些只需要在单页模式下做的事情，比如展示特定的宣传内容
			// } 
			var aid = '';
			var fuid = '';
			var stid = '';
			console.log('**************',options.scene)
			aid = options.aid ? options.aid : '';
			fuid = options.fuid ? options.fuid : '';
			stid = options.stid ? options.stid : '';
			// 如果没有传入aid，则默认进入上次的aid，上次的aid在app.mdInit函数中更新保存
			if(!aid){
					aid=wx.getStorageSync('VP_AID',2);
			}

			self.setData({
				aid:aid,
				fuid:fuid,
				stid:stid
			});
			console.log("==获取当前用户信息==");
			if(scene !== 1154){
				app.util.getUserInfo(function (response) {
					console.log("==获取当前用户信息1==");
					console.log(response);
					self.setData({
						userInfo: response
					});
					console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
					app.mdInit(function (cfg,mine,area) {
						self.setData({
							cfg: cfg,
							mine:mine,
							area:area
						});
						self.main_load(false);
				
						if(!aid){
							console.log("==无指定城市时，获取当前用户位置自动切换对应城市==");
							// 如果没有aid，则发起位置定位
							wx.getLocation({
								type: 'wgs84',
								success(res) {
									// 定位成功，获取最近的店，并提示是否切换
									//self.main_load(false);
									wx.setStorageSync('lng',res.longitude);
									wx.setStorageSync('lat',res.latitude);
									app.util.request({
										url: 'Index/location',
										showLoading:false,
										data: {
											m: app.mdName,
											//cmd:'location',
											lat:res.latitude,
											lng:res.longitude
										},
										success(res2) {
											var data = res2.data.data;
											console.log("==无指定城市时，定位到用户当前位置所在城市==");
											console.log(data);
				
											// 如果定位城市与当前城市不符，则推荐用户切换
											if(data.id!=area.id){
												wx.showModal({
													title: '切换城市',
													content: '您当前位于“'+data.name+'”',
													cancelText:'其他城市',
													confirmText:'确认进入',
													success (ret) {
														if (ret.confirm) {
															wx.reLaunch({
																//url: app.mdName + '/pages/index/index?aid='+data.id
																url: '../index/index?aid='+data.id
															});
														}else{
															wx.navigateTo({
																url: '../index/areas'
															})
														}
													}
												});
											}
										}
									})
								},
								fail(res) {
									console.log("fail");
									console.log(res);
									// 定位失败，获取默认的店，并提示是否切换
									//self.main_load(false);
								}
							});
						}
						
						if(self.data.vipcode != ""){
							self.checkvipcode(self.data.vipcode)
						}
					},{
							aid:self.data.aid,
							fuid:self.data.fuid,
							stid:self.data.fuid
					},true);
				});
			}
			var menu = wx.getMenuButtonBoundingClientRect();
			this.setData({
				menu:menu
			});
			console.log("menu==>");
			console.log(this.data.vipcode);
			
                
        },
		
        // 顶部导航栏初始化完毕
        onNavBarAttached:function(e){
			this.setData({
				statusHeight: e.detail.statusHeight,
				navHeight: e.detail.navHeight,
				navBarHeight: e.detail.navBarHeight
			});
			const self = this
			console.log("--------------------------------" + e.detail.navBarHeight);
			
		},

        onReady: function () {

  
                var self = this;

                var addmy = wx.getStorageSync('vp_pz_sharer_addmy');
                this.setData({
                        addmy:addmy?false:true
                });

                var noticed = wx.getStorageSync('vp_pz_noticed');
                if(noticed>0){
                        this.setData({
                                noticed:noticed
                        });  
                }
  
        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function (res) {
			
                // if(this.data.main_loaded){
                //     this.main_load(false);
                // }
        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },


        onSlideTap:function(e){
                var slide = this.data.slides[e.currentTarget.dataset.index];
                if(!slide){
                        return;
                }
                if(slide.stype==1){
                        wx.navigateTo({
                          url: slide.stype_link,
                        });
                }else if(slide.stype==2){
                        wx.navigateTo({
                                url: 'html?cmd=slide&id='+slide.id
                        });
                }else if(slide.stype==3){
                        console.log(slide.stype_link);
                        var link=slide.stype_link.split(":");
                        console.log(link);
                        wx.navigateToMiniProgram({
                                appId: link[0],
                                path: link[1]
                        });
                }else if(slide.stype==4){
                        console.log(slide.stype_link);
                        var link=slide.stype_link.split(":");
                        console.log(link);
                        wx.openChannelsActivity({
                                finderUserName: link[0],
                                feedId: link[1]
                        });
                }
        },

        
        onNav2Tap:function(e){
                var nav = this.data.nav2s[e.currentTarget.dataset.index];
                if(!nav){
                        return;
                }
				console.log(nav)
                if(nav.stype==1){
                        wx.navigateTo({
                          url: nav.stype_link,
                        });
                }else if(nav.stype==2){
                        wx.navigateTo({
                                url: 'html?cmd=nav&id='+nav.id
                        });
                }else if(nav.stype==3){
                        console.log(nav.stype_link);
                        var link=nav.stype_link.split(":");
                        console.log(link);
                        wx.navigateToMiniProgram({
                                appId: link[0],
                                path: link[1]
                        });
                }else if(nav.stype==4){
                        console.log(nav.stype_link);
                        var link=nav.stype_link.split(":");
                        console.log(link);
                        wx.openChannelsActivity({
                                finderUserName: link[0],
                                feedId: link[1]
                        });
                }
        },

        onNavTap:function(e){
                var nav = this.data.navs[e.currentTarget.dataset.index];
                if(!nav){
                        return;
                }
                if(nav.stype==1){
                        wx.navigateTo({
                          url: nav.stype_link,
                        });
                }else if(nav.stype==2){
                        wx.navigateTo({
                                url: 'html?cmd=nav&id='+nav.id
                        });
                }else if(nav.stype==3){
                        console.log(nav.stype_link);
                        var link=nav.stype_link.split(":");
                        console.log(link);
                        wx.navigateToMiniProgram({
                                appId: link[0],
                                path: link[1]
                        });
                }else if(nav.stype==4){
                        console.log(nav.stype_link);
                        var link=nav.stype_link.split(":");
                        console.log(link);
                        wx.openChannelsActivity({
                                finderUserName: link[0],
                                feedId: link[1]
                        });
                }
        },


        /**
         * 分享
        */
        onShareAppMessage: function () {
                console.log(this.getSharePath());
                return {
                        title: this.data.cfg.share_title?this.data.cfg.share_title:this.data.cfg.name,
                        imageUrl: this.data.cfg.share_image_url,
                        path: this.getSharePath()
                }
        },
   //      onShareTimeline:function(){
			// console.log('/vp_pz/pages/xuanchuan/promote')
   //          return {
   //              title: this.data.cfg.share_title?this.data.cfg.share_title:this.data.cfg.name,
   //              imageUrl: this.data.shareImage,
			// 	query:'aid='+this.data.area.id+'&fuid=' + this.data.mine.id
   //          }
   //      },
        getSharePath:function(){
                var page = app.mdName + '/pages/index/index';
                var ps = '?aid='+this.data.area.id+'&fuid=' + this.data.mine.id
                return page + ps;
        },

        showShareModal(){
                this.setData({_shareModal:true});
        },
		checkvipcode(vipcode){
			const self = this;
			//console.log("self.data.like "+self.data.like);
			app.util.request({
				url: 'Index/checkvip',
				data: {
					vipcode:vipcode
				},
				success(res) {
					console.log('_____________________________')
					var data = res.data.data;
					console.log(data)
					if(!data.is_empty){
						if(data.status){
							self.setData({
							  dialogShow: true,
							  msgInfo:"您扫描的会员码已经被激活，请确认后重试",
							  vipstatus:0
							});	
						}else{
							self.setData({
							  dialogShow: true
							});	
						}
					}else{
						self.setData({
						  dialogShow: true,
						  msgInfo:"您扫描的会员码有异常，请确认后重试",
						  vipstatus:0
						});	
					}
				},
				fail(e){
					console.log('___________************__________________')
					console.log(e)
				}
			})
		},
        // 切换区域
        toAreas:function(){
                wx.navigateTo({
                        url: '../index/areas',
                })
        },

        // 前往医院
        toHospital:function(e){
                wx.navigateTo({
                        url: '../index/hospital?hid='+e.currentTarget.dataset.hid,
                })
        },


        // 页面数据加载
        main_load(showLoading) {
			const self = this;
			//console.log("self.data.like "+self.data.like);
			app.util.request({
				url: 'Index/index',
				showLoading: showLoading ? true : false,
				data: {
						m: app.mdName,
						aid:self.data.area.id
				},
				complete(res) {
				wx.stopPullDownRefresh();
				},
				success(res) {
					var data = res.data.data;
					wx.setStorageSync('index_slides',data.slides)
					self.setData({
						main_loaded: true,
						now:data.now,
						slides:data.slides,
						nav2s:data.nav2s,
						navs:data.navs,
						hospitals:data.hospitals,
						my_staff:data.my_staff
					});
				}
			})
        },

        makePhoneCall:function(e){
                wx.makePhoneCall({
                        phoneNumber: e.currentTarget.dataset.tel
                });
        },


        closeAddmy(){
                this.setData({
                        addmy:false
                });
                wx.setStorage({
                        key:"vp_pz_sharer_addmy",
                        data:true
                });
        },
	// 弹框取消按钮回调
	  onDialogCancel() {
		console.log('用户点击了取消');
		this.setData({
		  dialogShow: false
		});		
		wx.removeStorageSync('vipcode')
	  },

	  // 弹框确定按钮回调
	  onDialogConfirm() {
		console.log('用户点击了确定');
		if(this.data.vipstatus == 1){
			wx.setStorageSync('vipcode',this.data.vipcode)
			//todo 
			//激活或者续费
			wx.showLoading()
			let that =this
			app.util.request({
				url: 'Index/jihuovip',
				data: {
					vipcode:that.data.vipcode
				},
				success(res) {
					console.log('_____________________________')
					wx.hideLoading()
					var data = res.data.data;
					console.log(data)
					if(data.is_status == 0){						
						wx.setStorageSync('VIP_STASTUS', 1);
						that.setData({
						  dialogShow: false
						});
						wx.redirectTo({
							url: '../familydoctor/index',
						})
					}else{
						that.setData({
						  dialogShow: false
						});
						wx.showToast({
						  title: data.msg || '激活失败',
						  icon: 'none'
						})
					}
				},
				fail(e){
					console.log('___________************__________________')
					console.log(e)
				}
			})
		}
		this.setData({
		  dialogShow: false
		});
	  }
        
})